<?php 

class pictureController extends appController
{
	
	public function clear()
	{
		unset($_SESSION['wxid']);
	}
	
	public function index()
	{
		$this->check_member();
		
// 		$_SESSION['wxid'] = 1;
// 		$this->check_member();
		
		$config[c] = array('site_name' => "报料-".Model('app')->get_cfg('site_name'));
		$this->assign_global($config);
		
		$jsdk = Model('suipai')->get_jsdk();
		
		$this->assign("jsdk", $jsdk);
		
		$this->display("index.html");
	}
	
	
	
	public function index2()
	{
	    $this->check_member();
	
	    // 		$_SESSION['wxid'] = 1;
	    // 		$this->check_member();
	
	    $config[c] = array('site_name' => "报料-".Model('app')->get_cfg('site_name'));
	    $this->assign_global($config);
	
	    $jsdk = Model('suipai')->get_jsdk();
	
	    $this->assign("jsdk", $jsdk);
	    
	    $jsdk = Model('suipai')->get_jsdk_card();
	    
	    $this->assign("jsdk_card", $jsdk);
	
	    $this->display("index2.html");
	}
	
	
	public function down()
	{
	
		$this->check_member();
		// 		$_SESSION['wxid'] = 1;
		// 		$this->check_member();
	
		$config[c] = array('site_name' => "报料-".Model('app')->get_cfg('site_name'));
		$this->assign_global($config);
	
				$jsdk = Model('suipai')->get_jsdk();
	
				$this->assign("jsdk", $jsdk);
	
		$this->display("down.html");
	}
	
	public function period()
	{
		
		$this->check_member();
// 		$_SESSION['wxid'] = 1;
// 		$this->check_member();
		
		$config[c] = array('site_name' => "报料-".Model('app')->get_cfg('site_name'));
		$this->assign_global($config);
		
		$jsdk = Model('suipai')->get_jsdk();
		
		$this->assign("jsdk", $jsdk);
		
		$this->display("period.html");
	}
	
	public function other()
	{
	
		
		// 		$_SESSION['wxid'] = 1;
		// 		$this->check_member();
		$this->check_member();
		$config[c] = array('site_name' => "报料-".Model('app')->get_cfg('site_name'));
		$this->assign_global($config);
	
		$jsdk = Model('suipai')->get_jsdk();
		$list = Model('suipai')->get_list();
		$list2 = Model('suipai')->get_list_parise();
		$this->assign("jsdk", $jsdk);
		$this->assign("list", $list);
		$this->assign("list2", $list2);
		$this->display("other.html");
	}
	
	public function upload()
	{
	
	
				
				$this->check_member();
	
		$config[c] = array('site_name' => "报料-".Model('app')->get_cfg('site_name'));
		$this->assign_global($config);
	
				$jsdk = Model('suipai')->get_jsdk();
	
				$this->assign("jsdk", $jsdk);
	
		$this->display("upload.html");
	}
	
	//点赞
	public function send_praise()
	{
		$res = array('error' => 1, 'content' => '');
		$info['wxid'] = $_SESSION['wxid'];
		$info['pid'] = $_POST['pid']?$_POST['pid']:0;
		
		$res['error'] = 1;
		$res['content'] = "活动已结束。";
		$this->showMsg($res);
		exit;
		if($info['pid'] ==0||$info['wxid'] ==0)
		{
			$res['content'] = "非法操作。";
			$this->showMsg($res);
		}
		
		$is_exist = Model('suipai')->check_is_praise($info['wxid'],$info['pid']);
		if($is_exist > 0)
		{
			$res['content'] = "你已经为该作品点过赞了。";
			$this->showMsg($res);
		}
		else
		{
			$info['addtime'] = time();
			Model('suipai')->insert_praise($info);
			
			$res['error'] = 0;
			$res['content'] = "点赞成功！";
			$this->showMsg($res);
		}
	}
	
	
	//上传
	public function send_set()
	{
		$res = array('error' => 1, 'content' => '');
		$info['wxid'] = $_SESSION['wxid'];
		$is_exist = Model('suipai')->check_is_picture($info['wxid']);
		if($is_exist > 0)
		{
			$res['content'] = "你已经提交过作品了。";
			$this->showMsg($res);
		}
		$info['content'] = yf_empty($_POST['content'], "请输入昵称");
		$info['phone'] = yf_empty($_POST['phone'], "请输入手机号");
		if(hy_check($info['phone'], 'm') == '#@_error' && hy_check($info['phone'], 't') == '#@_error')
		{
			$res['content'] = "你输入的电话格式不正确";
			$this->showMsg($res);
		}
		$img = yf_empty($_POST['img'], "请选择你要上传的图片");
		$arr = array();
		if(!is_array($img))
		{
			$arr[] = $img;
		}
		else
		{
			$arr = $img;
		}
		$info['pic'] = $img;
		$info['created'] = yf_time();
		$id = Model('suipai')->insert_record($info,"picture");
		
		
		if($id)
		{
			$count = Model('suipai')->getCount();
			$res = array('error' => 0, 'content' => '提交成功','count'=>$count);
		}
		else 
		{
			$res['content'] = "系统繁忙";
		}
		$this->showMsg($res);
	}
	
	public function upload_img()
	{
		$res = array('error' => 1, 'content' => '');
		$media_id = yf_empty($_POST['img'], "error");
		$token = Model('suipai')->getAccessToken2();
		$url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$token."&media_id=".$media_id;
		
		$json = file_get_contents($url);
		$arr = json_decode($json,true);
		
		
		$open=fopen("log.txt","a" );
		fwrite($open,$url."\r\n");
		fclose($open);
		$img = yf_time().".jpg";
		$img = $this->GrabImage($url, $img);
		$temp = str_replace(ROOT_PATH, "/", $img);
		$html = "<input type=\"hidden\" name=\"img\" value=\"$temp\">";
		$url = "<img src=\"$temp\"   class=\"pre-img\"    >";
		$res = array('error' => 0, 'content' => $html , 'url' =>$url ,'img'=>'http://g4.airefp.com/ws/'.$temp);
		$this->showMsg($res);
	}
	
	public function GrabImage($url, $filename = "") {
		if($url == ""):return false;endif;
		if($filename == "") {
			$ext = strrchr($url, ".");
			if($ext != ".gif" && $ext != ".jpg"):return false;endif;
			$filename = date("YmdHis").$ext;
		}
		ob_start();
		readfile($url);
		$img = ob_get_contents();
		ob_end_clean();
		$size = strlen($img);
// 		$file = ROOT_PATH.'Uploads/img/'.date('Ym');
		$file = 'Uploads/img/'.date('Ym');
		if(!is_dir($file))
		{
			mkdir($file);
		}
		$file = $file."/".$filename;
		$fp2 = @fopen($file, "a");
		fwrite($fp2, $img);
		fclose($fp2);
		return $file;
	}
}

?>